package com.emenu;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.emenu.application.StaticObject;
import com.emenu.bean.BusHitDetail;
import com.emenu.bean.Menu;
import com.emenu.bean.StackBean;
import com.emenu.bean.SubMenu;
import com.emenu.bean.User;
import com.emenu.common.SaveMenuImg;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

public class FragmentHit extends Fragment {
	private String submenu_code;
	private List<SubMenu> sublist = new ArrayList<SubMenu>();
	private String SDPATH;
	private SubMenu sm = new SubMenu();
	public StaticObject SO;

	Bitmap bm;

	private Button hitleftimg;
	private ImageView hit_img;
	private Button hitrightimg;
	private TextView hitintroduce_dtl;
	private TextView hitprice;
	private TextView hitindex;
	private TextView hit_today_hit;
	private TextView hit_his;
	public EditText hit_amount;
	private Button hit_post;
	private EditText hit_remark;

	List<BusHitDetail> bhdList;

	Context cn;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		SO = (StaticObject) activity.getApplication();
		cn = this.getActivity();
		bhdList = SO.getBusHitDetailList();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.hit, container, false);

		SDPATH = Environment.getExternalStorageDirectory() + File.separator
				+ "Picture" + File.separator;

		findViewsById(v);
		setListener();

		hit_amount.setText((int) getArguments().getDouble("amount") + "");
		showImg(sm);

		return v;
	}

	//
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		submenu_code = getArguments().getString("code");
		sublist = SO.getSubMenuList();
		for (SubMenu smenu : sublist) {
			if (smenu.getCode() == submenu_code) {
				sm = smenu;
			}
		}
		StackBean sb = new StackBean();
		sb.setName(sm.getName());
		SO.getStackList().add(sb);

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		((FragmentMAin) cn).showpath();
	}

	private void findViewsById(View v) {
		hit_img = (ImageView) v.findViewById((R.id.hit_img));
		hitleftimg = (Button) v.findViewById((R.id.hitleftimg));
		hitrightimg = (Button) v.findViewById((R.id.hitrightimg));
		hit_post = (Button) v.findViewById((R.id.hit_post));

		hitintroduce_dtl = (TextView) v.findViewById((R.id.hitintroduce_dtl));
		hitprice = (TextView) v.findViewById((R.id.hitprice));
		hitindex = (TextView) v.findViewById((R.id.hitindex));
		hit_today_hit = (TextView) v.findViewById((R.id.hit_today_hit));
		hit_his = (TextView) v.findViewById((R.id.hit_his));

		hit_amount = (EditText) v.findViewById((R.id.hit_amount));
		hit_remark = (EditText) v.findViewById((R.id.hit_remark));
	}

	/** 设置监听器 */
	private void setListener() {
		hitleftimg.setOnClickListener(leftimg);
		hitrightimg.setOnClickListener(rightimg);
		hit_post.setOnClickListener(post);
	}

	private OnClickListener leftimg = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (null != bm && !bm.isRecycled()) {
				bm.recycle();
			}

			// 当前对象的索引
			int i = sublist.indexOf(sm);

			if (i > 0) {
				sm = sublist.get(i - 1);
				showImg(sm);
			}

		}
	};
	private OnClickListener rightimg = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (null != bm && !bm.isRecycled()) {
				bm.recycle();
			}
			// 当前对象的索引
			int i = sublist.indexOf(sm);

			if (i < sublist.size()) {
				sm = sublist.get(i + 1);
				showImg(sm);
			}
		}
	};
	// 点菜
	private OnClickListener post = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// 已点菜单传过来参数为double，要转int
			int ha = (int) (Double.parseDouble(String.valueOf(hit_amount
					.getText())));
			String rm = String.valueOf(hit_remark.getText()).trim();

			int itemp = 0;
			// 原来已点过该菜品，直接在全局list里吧把该菜品数量加所点数量
			for (BusHitDetail bhd : bhdList) {

				if (bhd.getMenucode().equals(sm.getCode())) {
					bhd.setAmount(bhd.getAmount() + ha);
					itemp = 1;
					break;
				}
			}

			// 原来未点过该菜品，list里新加入该条记录
			if (itemp != 1) {
				BusHitDetail bhd = new BusHitDetail();
				bhd.setBig_Menu_code(sm.getMenu_code());

				bhd.setID(-1);
				bhd.setHit_code("-1");
				bhd.setMenucode(sm.getCode());
				bhd.setMenuname(sm.getName());
				bhd.setPrice(sm.getPrice());
				bhd.setAmount(ha);
				bhd.setTag_code(sm.getTag_code());
				bhd.setTag_name(sm.getTag_name());
				bhd.setAmount_sum(Math.round(sm.getPrice() * ha));
				bhd.setMenu_type("-1");
				bhd.setRljtype(sm.getRljtype());
				bhd.setCalledSingle(0);
				bhd.setRemark(rm);
				bhd.setUpload(0);
				bhd.setC1("");
				bhd.setC2("");
				bhd.setC3("");

				bhdList.add(bhd);
				SO.setBusHitDetailList(bhdList);
			}
			// 刷新右边树形菜单
			for (Menu mu : SO.getMenuList()) {

				if (mu.getCode().equals(sm.getMenu_code())) {
					mu.setCount(mu.getCount() + ha);
				}
				if (mu.getC1() == sm.getRljtype()) {
					mu.setCount(mu.getCount() + ha);
				}
			}
			((FragmentMAin) cn).refreshRight();

		}
	};

	public void showImg(SubMenu sum) {

		String fileName = SDPATH + SaveMenuImg.getpath(sum.getPicpath())
				+ sum.getName() + ".jpg";
		bm = BitmapFactory.decodeFile(fileName);
		if (bm == null) {
			bm = BitmapFactory.decodeStream(getResources().openRawResource(
					R.drawable.imgdefault));
		}
		hit_img.setImageBitmap(bm);
		hitintroduce_dtl.setText(sm.getRemark());
		hitprice.setText(sm.getPrice() + "/" + sm.getTag_name());
		hitindex.setText(sm.getHitamount() + "");

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		// 释放图片内存
		if (!bm.isRecycled()) {
			bm.recycle();
		}
	}

}
